package com.c3w.synchro.member.validator;

import com.c3w.synchro.common.dao.BeanDao;
import com.c3w.synchro.common.validation.ValidationResult;
import com.c3w.synchro.common.validation.Validator;
import com.c3w.synchro.common.validation.event.ValidationFailedException;
import com.c3w.synchro.member.bean.Athlete;
import com.c3w.synchro.member.validator.MemberValidator;
import org.apache.commons.validator.GenericValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * User: magirard
 * Date: 12-11-15
 */
public class AthleteValidator extends MemberValidator<Athlete> {
    @Override
    public void validate(Athlete bean) throws ValidationFailedException {
        ValidationResult result = new ValidationResult();
        try{
            super.validate(bean);
        }catch ( ValidationFailedException e )
        {
            result = e.getResult();
        }


        // Verifie l'existance du bean
        if (bean != null) {
            // TODO Valider la presence du club.

            // 2 - Validation de l'age
            Integer age = bean.getAge();
            if (age == null) {
                result.add("age", "Age must be provided");
            }

        }


        if (result.getErrorCount() > 0) {
            throw new ValidationFailedException(result);
        }
    }
}
